class Node {
    private int data;
    private Node next;

    public Node(int i, Node nt) {
        data = i;
        next = nt;
    }

    public Node(int i) {
        this(i, null);
    }

    public Node() {
        this(0, null);
    }

    // Getter/Setter 方法
    public int getData() {
        return data;
    }
    public void setData(int i) {
        data = i;
    }
    public Node getNext() {
        return next;
    }
    public void setNext(Node nt) { next = nt; }
}

public class LinkedList {
    private Node head;
    private Node tail;
    private int size;

    public LinkedList() {
        head = tail = null;
        size = 0;
    }

    // 头插法（保持循环特性）
    public void addHead(Node hd) {
        if (size == 0) {
            hd.setNext(hd); // 自循环
            head = tail = hd;
        } else {
            hd.setNext(head);
            tail.setNext(hd);
            head = hd;
        }
        size++;
    }

    // 尾插法（保持循环特性）
    public void addTail(Node tl) {
        if (size == 0) {
            tl.setNext(tl); // 自循环
            head = tail = tl;
        } else {
            tail.setNext(tl);
            tl.setNext(head);
            tail = tl;
        }
        size++;
    }

    // 删除头节点
    public void delHead() {
        if (size == 0) {
            System.out.println("链表为空");
            return;
        }
        if (size == 1) {
            head = tail = null;
        } else {
            head = head.getNext();
            tail.setNext(head);
        }
        size--;
    }

    // 删除尾节点
    public void delTail() {
        if (size == 0) {
            System.out.println("链表为空");
            return;
        }
        if (size == 1) {
            head = tail = null;
        } else {
            Node current = head;
            while (current.getNext() != tail) {
                current = current.getNext();
            }
            current.setNext(head);
            tail = current;
        }
        size--;
    }

    // 新增：删除指定值的节点
    public void deleteNode(int data) {
        if (size == 0) {
            System.out.println("链表为空");
            return;
        }

        Node current = head;
        Node prev = tail; // 循环链表需要尾节点作为前驱

        do {
            if (current.getData() == data) {
                // 找到目标节点
                if (current == head) {
                    delHead(); // 删除头节点
                } else if (current == tail) {
                    delTail(); // 删除尾节点
                } else {
                    prev.setNext(current.getNext());
                    size--;
                }
                return;
            }
            prev = current;
            current = current.getNext();
        } while (current != head);

        System.out.println("未找到数据为" + data + "的节点");
    }

    // 新增：反转循环链表
    public void reverseList() {
        if (size <= 1) return;

        Node prev = tail;
        Node current = head;
        Node next;

        do {
            next = current.getNext();
            current.setNext(prev);
            prev = current;
            current = next;
        } while (current != head);

        // 交换头尾节点
        Node temp = head;
        head = tail;
        tail = temp;
    }

    // 修复后的打印方法（解决死循环）
    public void printList() {
        if (size == 0) {
            System.out.println("[]");
            return;
        }

        System.out.print("[");
        Node current = head;
        do {
            System.out.print(current.getData());
            if (current.getNext() != head) {
                System.out.print(" -> ");
            }
            current = current.getNext();
        } while (current != head);
        System.out.println("]");
    }
}

class LinkedlistTest {
    public static void main(String[] args) {
        LinkedList lst = new LinkedList();

        // 测试头插法
        lst.addHead(new Node(15));
        lst.addHead(new Node(14));
        lst.addHead(new Node(13));
        lst.addTail(new Node(21));
        lst.addTail(new Node(22));
        lst.addTail(new Node(23));

        System.out.println("初始链表：");
        lst.printList(); // [13 -> 14 -> 15 -> 21 -> 22 -> 23]

        // 测试删除节点
        System.out.println(" 删除节点13：");
                lst.deleteNode(13);
        lst.printList(); // [14 -> 15 -> 21 -> 22 -> 23]

        System.out.println(" 删除节点23：");
                lst.deleteNode(23);
        lst.printList(); // [14 -> 15 -> 21 -> 22]

        // 测试反转链表
        System.out.println(" 反转链表：");
                lst.reverseList();
        lst.printList(); // [22 -> 21 -> 15 -> 14]
    }
}